home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / stdlib / getenv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-22  |  1.8 KB  |  65 lines

  1. /* 
  2.  * getenv.c --
  3.  *
  4.  *    Source code for the "getenv" library procedure.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/getenv.c,v 1.2 89/03/22 00:47:13 rab Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. extern char **environ;
  24.  
  25. /*
  26.  *----------------------------------------------------------------------
  27.  *
  28.  * getenv --
  29.  *
  30.  *    Locate an environment variable by a given name.
  31.  *
  32.  * Results:
  33.  *    The return value is a pointer to the value associated with
  34.  *    name, or 0 if there is no value registered for name.  The return
  35.  *    value points into environment storage, which is only guaranteed
  36.  *    to persist until the next call to setenv.
  37.  *
  38.  * Side effects
  39.  *    None.
  40.  *
  41.  *----------------------------------------------------------------------
  42.  */
  43.  
  44. char *
  45. getenv(name)
  46.     char    *name;        /* Name to retrieve value for. */
  47. {
  48.     char **envPtr;        /* pointer into list of environ. vars. */
  49.     register char *charPtr;    /* point into one environment variable */
  50.     register char *namePtr;    /* pointer into name */
  51.  
  52.     for (envPtr = environ; *envPtr != NULL; envPtr++) {
  53.     for (charPtr = *envPtr, namePtr = name; *charPtr == *namePtr;
  54.         charPtr++, namePtr++) {
  55.         if (*charPtr == '=') {
  56.         break;
  57.         }
  58.     }
  59.     if ((*charPtr == '=') && (*namePtr == NULL)) {
  60.         return charPtr+1;
  61.     }
  62.     }
  63.     return NULL;
  64. }
  65.